package cn.com.yusys.yusp.admin.service;

import cn.com.yusys.yusp.admin.domain.AdminSmInstu;
import cn.com.yusys.yusp.admin.domain.AdminSmOrg;
import cn.com.yusys.yusp.admin.repository.mapper.AdminSmInstuMapper;
import cn.com.yusys.yusp.admin.repository.mapper.AdminSmOrgMapper;
import cn.com.yusys.yusp.commons.mapper.CommonMapper;
import cn.com.yusys.yusp.commons.mapper.QueryModel;
import cn.com.yusys.yusp.commons.service.CommonService;
import cn.com.yusys.yusp.commons.util.DateUtil;

import com.github.pagehelper.PageHelper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * 
 * @项目名称: yusp-admin
 * @类名称: AdminSmInstuService
 * @类描述: 金融机构service
 * @功能描述: 
 * @创建人: hujun3@yusys.com.cn
 * @创建时间: 2017-12-6 15:09
 * @修改备注: 
 * @修改记录: 修改时间    修改人员    修改原因
 * -------------------------------------------------------------
 * @version 1.0.0
 * @Copyright (c) 2017宇信科技-版权所有
 */
@Service
public class AdminSmInstuService extends CommonService {
	private Logger logger = LoggerFactory.getLogger(AdminSmInstuService.class);

	@Autowired
	private AdminSmInstuMapper mapper;
	@Autowired
	private AdminSmOrgMapper orgMapper;

	public AdminSmInstuService(AdminSmInstuMapper commonMapper) {
		super();
		this.mapper=commonMapper;
	}
	
	@Override
	protected CommonMapper<?> getMapper() {
		return this.mapper;
	}

	/**
	 * 
	* @方法名称: insertInfo
	* @方法描述: 新增金融机构数据
	* @参数与返回说明: 
	* @算法描述:
	 */
	@Transactional(readOnly = false, rollbackFor = { Exception.class, RuntimeException.class })
	public Map<String,Object> insertInfo(AdminSmInstu pool) {
		Map<String,Object> result=new HashMap<String, Object>();
		Map<String, String> paramMap=new HashMap<String, String>();
		paramMap.put("instuCde", pool.getInstuCde());
		paramMap.put("sysId", pool.getSysId());
		List<Map<String, Object>> res=mapper.findInstuByParam(paramMap);
		
		if(res.size()==0) {
			pool.setLastChgDt(DateUtil.formatDateTimeByDef());//最近跟新时间
			if(pool.getJoinDt()!=null&&!"".equals(pool.getJoinDt())) {
				pool.setJoinDt(pool.getJoinDt().substring(0, 10));
			}
			if(pool.getInstuSts()==null||"".equals(pool.getInstuSts())) {
				pool.setInstuSts("W");//新增的数据都是待生效的
			}
			if(pool.getJoinDt()!=null&&!"".equals(pool.getJoinDt())) {
				pool.setJoinDt(pool.getJoinDt().substring(0, 10));
			}
			logger.info("New data of financial institutions: [new financial institutions: {}]", pool.getInstuName());
			this.insertSelective(getMapper(), pool);
			List<Map<String, Object>> rootOrg= orgMapper.getRootOrgInfo();
			if(rootOrg!=null&&rootOrg.size()>0) {
				AdminSmOrg org=new AdminSmOrg();
				org.setInstuId(pool.getInstuId());
				org.setOrgLevel("2");
				org.setOrgName(pool.getInstuName());
				org.setOrgSts("W");//默认生成的机构数据是待生效的
				org.setLastChgDt(pool.getLastChgDt());
				org.setLastChgUsr(pool.getLastChgUsr());
				org.setUpOrgId(rootOrg.get(0).get("orgId").toString());//一级机构的根节点编号
				org.setOrgCode(pool.getInstuCde()+"0001");//新增的机构编号在金融机构编号的基础上加上:0001
				org.setOrgId(pool.getInstuCde()+"0001");
				//新增金融机构后自动新增对应的机构信息
				orgMapper.insertSelective(org);
			}
			
			result.put("code", "200");
			result.put("message", "金融机构代码为："+pool.getInstuCde()+",金融机构名称为:"+pool.getInstuName()+"的数据保存成功。");
		}else {//如果已存在就不能新增
			result.put("code", "2");
			result.put("message", "金融机构代码为："+pool.getInstuCde()+",金融机构名称为:"+pool.getInstuName()+"的数据已经存在。");
		}
		
		return result;
	}
	
	/**
	 * 
	* @方法名称: queryPage
	* @方法描述: 分页查询
	* @参数与返回说明: 
	* @算法描述:
	 */
	@Transactional(readOnly = true)
	public List<Map<String, Object>> queryPage(QueryModel model){
		// 设置分页查询参数(设置到线程变量中了)
		PageHelper.startPage(model.getPage(), model.getSize());
		model.setSort("last_chg_dt desc");
		List<Map<String, Object>> list=this.mapper.queryInstuByPage(model);
		PageHelper.clearPage();
		return list;
	}
	
	/**
	 * 
	* @方法名称: queryOrgByInstuId
	* @方法描述: 查询金融机构下的机构
	* @参数与返回说明: 
	* @算法描述:
	 */
	@Transactional(readOnly = true)
	public int queryOrgByInstuId(String instuId){
		Map<String, String> paramMap=new HashMap<String, String>();
		paramMap.put("instuId", instuId);
		List<Map<String, Object>> list=this.mapper.findOrgByInstu(paramMap);
		int result=0;
		if(list!=null) {
			result=list.size();
		}
		return result;
	}
	
	/**
	 * 
	* @方法名称: queryPage
	* @方法描述:  修改金融机构数据
	* @参数与返回说明: 
	* @算法描述:
	 */
	@Override
	@Transactional(readOnly = false, rollbackFor = { Exception.class, RuntimeException.class })
	public int update(Object record) {
		AdminSmInstu pool=(AdminSmInstu)record;
		AdminSmInstu pool1=this.selectByPrimaryKey(pool.getInstuId());
		pool.setLastChgDt(DateUtil.formatDateTimeByDef());//最近跟新时间
		if(pool.getJoinDt()!=null&&!"".equals(pool.getJoinDt())) {
			pool.setJoinDt(pool.getJoinDt().substring(0, 10));
		}
		if(pool.getInstuSts()==null||"".equals(pool.getInstuSts())) {
			pool.setInstuSts("W");//
		}
		if(pool.getJoinDt()!=null&&!"".equals(pool.getJoinDt())) {
			pool.setJoinDt(pool.getJoinDt().substring(0, 10));
		}
		if(!pool.getInstuName().equals(pool1.getInstuName())) {
			AdminSmOrg orgInfo = orgMapper.selectByPrimaryKey(pool.getInstuCde()+"0001");
			orgInfo.setOrgName(pool.getInstuName());
			orgInfo.setLastChgDt(DateUtil.formatDateTimeByDef());//最近跟新时间
			orgMapper.updateByPrimaryKeySelective(orgInfo);
		}
		logger.info("Modified data of financial institutions: [modify financial institutions: {}] ", pool.getInstuName());
		return this.updateSelective(getMapper(), pool);
	}
	
	/**
	 * 
	* @方法名称: useIngFn
	* @方法描述: 启用金融机构
	* @参数与返回说明: 
	* @算法描述:
	 */
	public int useIngFn(String id,String lastChgUsr) {
		int n=0;
		if(!"".equals(id)) {
			Map<String, String> paramMap=new HashMap<String, String>();
			paramMap.put("sts", "A");
			paramMap.put("id", id);
			paramMap.put("user",lastChgUsr);
			paramMap.put("date", DateUtil.formatDateTimeByDef());
			n=this.mapper.updateInstuSts(paramMap);
			Map<String, String> param=new HashMap<String, String>();
			param.put("sts", "A");
			param.put("instuId", id);
			param.put("user",lastChgUsr);
			param.put("date", DateUtil.formatDateTimeByDef());
			orgMapper.updateOrgSts(param);
		}
		return n;
	}
	
	/**
	 * 
	* @方法名称: unUseIngFn
	* @方法描述: 停用金融机构
	* @参数与返回说明: id(主键ID)
	* @算法描述:
	 */
	public int unUseIngFn(String id,String lastChgUsr) {
		int n=0;
		if(!"".equals(id)) {
			
			Map<String, String> paramMap=new HashMap<String, String>();
			paramMap.put("sts", "I");
			paramMap.put("id", id);
			paramMap.put("user", lastChgUsr);
			paramMap.put("date", DateUtil.formatDateTimeByDef());
			n=this.mapper.updateInstuSts(paramMap);
			Map<String, String> param=new HashMap<String, String>();
			param.put("sts", "I");
			param.put("instuId", id);
			param.put("user",lastChgUsr);
			param.put("date", DateUtil.formatDateTimeByDef());
			orgMapper.updateOrgSts(param);
		}
		return n;
	}
	/**
	 * 
	* @方法名称: deleteOrgAndInstu
	* @方法描述: 删除金融机构及下面的机构
	* @参数与返回说明: instuId(金融机构编号)
	* @算法描述:
	 */
	public int deleteOrgAndInstu(String instuId) {
		orgMapper.deleteByInstuId(instuId);
		return mapper.deleteByPrimaryKey(instuId);
	}
	
	
}
